package qc;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import org.quickconnect.ControlObject;
import org.quickconnect.QuickConnect;

import com.mysql.jdbc.Connection;

import controller.ServerConnectionHandler;
import controller.ServerController;

import ui.MainFrame;

import beans.CommunicationBean;
import beans.MessageBean;

public class BroadcastMessageBCO implements ControlObject {

	@Override
	public Object handleIt(ArrayList<Object> arg0) {
		Connection con = (Connection) MainFrame.mainFrame.getController().getConnectionPool().getConnection();
		ServerConnectionHandler connection = (ServerConnectionHandler) arg0.get(0);
		HashMap params = (HashMap) arg0.get(arg0.size()-1);
		MessageBean message = (MessageBean) params.get("message");
		String sessionNumber = message.getSessionid();
		CommunicationBean commBean = new CommunicationBean();
		commBean.setParameters(params);
		commBean.setCommand("receiveMessage");
		
		HashMap<String, HashMap<String, ObjectOutputStream>> sessions = MainFrame.mainFrame.getController().getConnectionMap();
		
		HashMap<String, ObjectOutputStream> session = sessions.get(sessionNumber);
		
		ArrayList streams = new ArrayList(session.values());

		if (params.containsKey("userTo")) {
			boolean sendMessage = true;
			if((Boolean)params.get("roomMuted") && !(Boolean)params.get("moderator"))
			{

				java.sql.PreparedStatement select = null;
				ResultSet results = null;
				try 
				{
					select = con.prepareStatement("SELECT u.UserName FROM Moderator m INNER JOIN User u ON m.UserID = u.UserID INNER JOIN SessionParticipants sp ON sp.UserID = m.UserID AND sp.SessionID=m.SessionID WHERE m.SessionID = (SELECT SessionID from Session WHERE SessionNumber = ?) AND u.UserName = ?");
					select.setString(1, sessionNumber);
					select.setString(2, (String)params.get("userTo"));
					results = select.executeQuery();

					if(!results.next())
					{
						ArrayList errors = new ArrayList();
						HashMap errorsMap = new HashMap();
						errorsMap.put("message", "This room is muted. You may only private message moderators.");
						errors.add(connection);
						errors.add(errorsMap);
						QuickConnect.handleError("sendError", errors);
						sendMessage = false;
					}
				}
				catch(SQLException e1)
				{
					e1.printStackTrace();
				}
			}

			if(sendMessage)
			{
				params.put("private", true);
				HashMap sessionsMap = MainFrame.mainFrame.getController().getConnectionMap();
				HashMap thisSessionMap = (HashMap) sessionsMap.get(message.getSessionid());
				ObjectOutputStream curStream = (ObjectOutputStream) thisSessionMap.get(params.get("userTo"));
				ObjectOutputStream myStream = (ObjectOutputStream)thisSessionMap.get(connection.getUser().getUsername());
				try {
					curStream.writeObject(commBean);
					myStream.writeObject(commBean);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		else if(!(Boolean)params.get("roomMuted") || (Boolean)params.get("moderator")) {
			for(int i = 0; i < streams.size(); i++){
				ObjectOutputStream curStream = (ObjectOutputStream) streams.get(i);
				try {
					curStream.writeObject(commBean);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return params;
	}

}
